package midas

import (
	"fmt"
	"sort"
	"strings"
	"crypto/hmac"
	"crypto/sha256"
	"io"
	"gitee.com/siruihuyu/tower/core/log"
)

//米大师支付签名
func signature(params map[string]interface{}, path string, secret string) string {
	keys := make([]string, 0, len(params))
	for k := range params {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	keyValueConcat := make([]string, 0, len(params))
	for _, k := range keys {
		keyValueConcat = append(keyValueConcat, fmt.Sprintf("%s=%v", k, params[k]))
	}
	keyValueStr := strings.Join(keyValueConcat, "&")

	//拼接请求参数和uri
	stringSignTemp := keyValueStr + path
	log.Info("stringSignTemp:%s",stringSignTemp)

	//获得签名字符串
	stringSig := getHmacCode(stringSignTemp,secret)
	return stringSig
}



//s 加密的的字符串
//key 加密的key--微信小游戏传米大师的key
func getHmacCode(s string,key string) string {
	h := hmac.New(sha256.New, []byte(key))
	io.WriteString(h, s)
	return fmt.Sprintf("%x", h.Sum(nil))
}

